---
title: Forecasting Monthly Expenditures with TimeGPT
sidebarTitle: House Sales with TimeGPT
---

## Introduction

In this tutorial, we introduce Nixtla’s TimeGPT integration which offers the first foundational model for time series forecasting. Follow along to see how it works.

## Prerequisites

### MindsDB Setup

One way is to sign up for an account at [MindsDB Cloud](https://cloud.mindsdb.com/). It is a convenient option as it doesn’t require any installation procedures. You can find the details [here](/setup/cloud).

Alternatively, visit our docs and follow the instructions to manually set up a local instance of MindsDB via [Docker](/setup/self-hosted/docker) or [pip](/setup/self-hosted/pip/source). You can also set up MindsDB on AWS following [this instruction set](https://aws.amazon.com/marketplace/pp/prodview-2nmhvskydmyrw).

### Creating an ML Engine

You can check the available engines with this command:

```sql
SHOW ML_ENGINES;
```

If you see the TimeGPT engine on the list, you are ready to follow the tutorials. If you do not see TimeGPT on the list, you will have to create an instance of the engine first with this command:

```sql
CREATE ML_ENGINE timegpt FROM timegpt USING timegpt_api_key = '...'
```

Notice that the `USING` clause is optional, but you must pass an API key eventually (either at model creation, engine creation, model usage, or in the mindsdb configuration file). The only exception to this is when using `cloud.mindsdb.com`. If you want your own token please register [here](https://www.nixtla.io/) (Submit Interest).


## Tutorial

### Connecting the Data

In this tutorial, we take our the Monthly Expenditures dataset.

We use a table from our MySQL public demo database, so let’s start by connecting MindsDB to it:

```sql
CREATE DATABASE mysql_demo_db
WITH ENGINE = 'mysql',
PARAMETERS = {
    "user": "user",
    "password": "MindsDBUser123!",
    "host": "db-demo-data.cwoyhfn6bzs0.us-east-1.rds.amazonaws.com",
    "port": "3306",
    "database": "public"
};
```

Now that we’ve connected our database to MindsDB, let’s query the data to be used in the example:

```sql
SELECT *
FROM mysql_demo_db.historical_expenditures
LIMIT 3;
```

Here is the output:

```sql
| month | category | expenditure |
| ----- | -------- | ----------- |
| 1982-04-01 | food | 1162.6 |
| 1982-05-01 | food | 1150.9 |
| 1982-06-01 | food | 1160 |
```

### Creating a Model

Let's create a model table to predict the expenditure values:

```sql
CREATE MODEL nixtla_timegpt_quarterly_expenditure_forecaster
FROM mysql_demo_db
  (SELECT * FROM historical_expenditures)
PREDICT expenditure
ORDER BY month
GROUP BY category
WINDOW 12
HORIZON 3
USING ENGINE = 'timegpt';
```

We add the `USING` clause that specifies the ML engine used to make predictions.

We can check the training status with the following query:

```sql
DESCRIBE nixtla_timegpt_quarterly_expenditure_forecaster;
```

### Making Predictions

Once the model status is `complete`, the behavior is the same as with any other AI table – you can query for batch predictions by joining it with a data table:

```sql
SELECT m.month as month, m.expenditure as forecasted
FROM nixtla_timegpt_quarterly_expenditure_forecaster as m
JOIN mysql_demo_db.historical_expenditures as t
WHERE t.month > LATEST
AND t.category = 'food';
```

Here is the output data:

```sql
| month | forecasted |
| ----- | ---------- |
| 2017-09-01 00:01:00.000000 | 10307.9423828125 |
| 2017-09-01 00:02:00.000000 | 10307.931640625 |
| 2017-09-01 00:03:00.000000 | 10307.9384765625 |
```

## What's Next?

Have fun while trying it out yourself!

- Bookmark [MindsDB repository on GitHub](https://github.com/mindsdb/mindsdb).
- Sign up for a free [MindsDB account](https://cloud.mindsdb.com/register).
- Engage with the MindsDB community on
  [Slack](https://mindsdb.com/joincommunity) or
  [GitHub](https://github.com/mindsdb/mindsdb/discussions) to ask questions and
  share your ideas and thoughts.

If this tutorial was helpful, please give us a GitHub star
[here](https://github.com/mindsdb/mindsdb).
